package com.zzyl.nursing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.*;
import com.zzyl.nursing.dto.FamilyElderDto;
import com.zzyl.nursing.mapper.*;
import com.zzyl.nursing.service.IFamilyMemberElderService;
import com.zzyl.nursing.vo.ElderReturnInfoVo;
import com.zzyl.nursing.vo.ElderlyInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 家属和老人关联Service业务层处理
 *
 * @author guomo
 * @date 2025-10-23
 */
@Service
public class FamilyMemberElderServiceImpl extends ServiceImpl<FamilyMemberElderMapper, FamilyMemberElder> implements IFamilyMemberElderService {
    @Autowired
    private FamilyMemberElderMapper familyMemberElderMapper;

    @Autowired
    private ElderMapper elderMapper;

    @Autowired
    private BedMapper bedMapper;

    @Autowired
    private RoomMapper roomMapper;

    @Autowired
    private DeviceMapper deviceMapper;

    /**
     * 查询家属和老人关联
     *
     * @param id 家属和老人关联主键
     * @return 家属和老人关联
     */
    @Override
    public FamilyMemberElder selectFamilyMemberElderById(Long id) {
        return getById(id);
    }

    /**
     * 查询家属和老人关联列表
     *
     * @param familyMemberElder 家属和老人关联
     * @return 家属和老人关联
     */
    @Override
    public List<FamilyMemberElder> selectFamilyMemberElderList(FamilyMemberElder familyMemberElder) {
        return familyMemberElderMapper.selectFamilyMemberElderList(familyMemberElder);
    }

    /**
     * 新增家属和老人关联
     *
     * @param familyMemberElder 家属和老人关联
     * @return 结果
     */
    @Override
    public int insertFamilyMemberElder(FamilyMemberElder familyMemberElder) {
        return save(familyMemberElder) ? 1 : 0;
    }

    /**
     * 修改家属和老人关联
     *
     * @param familyMemberElder 家属和老人关联
     * @return 结果
     */
    @Override
    public int updateFamilyMemberElder(FamilyMemberElder familyMemberElder) {
        return updateById(familyMemberElder) ? 1 : 0;
    }

    /**
     * 批量删除家属和老人关联
     *
     * @param ids 需要删除的家属和老人关联主键
     * @return 结果
     */
    @Override
    public int deleteFamilyMemberElderByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除家属和老人关联信息
     *
     * @param id 家属和老人关联主键
     * @return 结果
     */
    @Override
    public int deleteFamilyMemberElderById(Long id) {
        return removeById(id) ? 1 : 0;
    }

    @Override
    public void add(FamilyElderDto dto) {
        String idCard = dto.getIdCard();
        LambdaQueryWrapper<Elder> query = new LambdaQueryWrapper<>();
        query.eq(Elder::getIdCardNo,idCard);
        Elder elder = elderMapper.selectOne(query);
        if(elder!=null){
            LambdaQueryWrapper<FamilyMemberElder> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(FamilyMemberElder::getElderId,elder.getId());
            FamilyMemberElder familyMemberElder = familyMemberElderMapper.selectOne(queryWrapper);
            if(familyMemberElder!=null){
                throw new BaseException("该老人已被绑定");
            }
        }
        LambdaQueryWrapper<Elder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Elder::getIdCardNo, idCard);
        queryWrapper.eq(Elder::getName, dto.getName());
        //入住状态
        queryWrapper.eq(Elder::getStatus, 4);
        elder = elderMapper.selectOne(queryWrapper);
        if (elder == null) {
            throw new BaseException("要绑定的老人不存在");
        }
        FamilyMemberElder familyMemberElder = new FamilyMemberElder();
        familyMemberElder.setMemberId(UserThreadLocal.get());
        familyMemberElder.setElderId(elder.getId());
        familyMemberElder.setRemark(dto.getRemark());
        this.save(familyMemberElder);
    }

    @Override
    public List<ElderReturnInfoVo> findElder() {
        List<FamilyMemberElder> list = this.list();
        List<ElderReturnInfoVo> voList = new ArrayList<>();
        list.forEach(memberAndElder -> {
            ElderReturnInfoVo vo = new ElderReturnInfoVo();
            vo.setId(memberAndElder.getId());
            vo.setFamilyMemberId(memberAndElder.getMemberId());
            vo.setElderId(memberAndElder.getElderId());
            Elder elder = elderMapper.selectById(memberAndElder.getElderId());
            if (elder != null) {
                vo.setElderName(elder.getName());
            }
            voList.add(vo);
        });
        return voList;
    }

    @Override
    public List<ElderlyInfoVO> findListElder(Integer pageNum, Integer pageSize) {
        Long userId = UserThreadLocal.getUserId();
        PageHelper.startPage(pageNum, pageSize);
        List<FamilyMemberElder> familyMemberElderList = familyMemberElderMapper.findListElder(userId);

        List<ElderlyInfoVO> list = new ArrayList<>();
        familyMemberElderList.forEach(familyMemberElder->{
            ElderlyInfoVO elderlyInfoVO = new ElderlyInfoVO();
            elderlyInfoVO.setMid(familyMemberElder.getMemberId());
            elderlyInfoVO.setMremark(familyMemberElder.getRemark());
            elderlyInfoVO.setElderId(familyMemberElder.getElderId());
            Elder elder = elderMapper.selectById(familyMemberElder.getElderId());
            elderlyInfoVO.setName(elder.getName());
            elderlyInfoVO.setImage(elder.getImage());
            elderlyInfoVO.setBedNumber(elder.getBedNumber());

            Bed bed = bedMapper.selectById(elder.getBedId());
            Room room = roomMapper.selectById(bed.getRoomId());
            elderlyInfoVO.setTypeName(room.getTypeName());
            LambdaQueryWrapper<Device> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Device::getLocationType,0);
            queryWrapper.eq(Device::getRemark,elder.getName());
            List<Device> devices = deviceMapper.selectList(queryWrapper);
            devices.forEach(device -> {
                elderlyInfoVO.setIotId(device.getIotId());
                elderlyInfoVO.setDeviceName(device.getDeviceName());
                elderlyInfoVO.setProductKey(device.getProductKey());
            });
            list.add(elderlyInfoVO);
        });
        return list;
    }
}
